package net.zdreamer.geracomium.ui.more;

import java.util.ArrayList;

import net.zdreamer.geracomium.adapter.TreeLeafAdapter;
import net.zdreamer.geracomium.utils.PersonalinfoUtils.Leaf;
import net.zdreamer.geracomium.utils.PersonalinfoUtils.LeafDao;
import net.zdreamer.geracomium.utils.PersonalinfoUtils.TreeLeafItemClickListener;
import net.zdreamer.geracomium.utils.file.getAdress;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.geracomium.R;

public class ChooseAdressActivity extends Activity implements View.OnClickListener{
	private ArrayList<LeafDao> leafs;
	private ArrayList<LeafDao> leafData;
	private LayoutInflater inflater;
	public static ListView treeview;
	Intent intent;
	public String Index,source;
    private ImageView back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chooseadress);
        back=(ImageView)findViewById(R.id.back);
        back.setOnClickListener(this);
		intent=getIntent();
		 Index=intent.getStringExtra("info");
		 source=intent.getStringExtra("source");
		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		queryDate();

		treeview = (ListView) findViewById(R.id.lv_lists);

		TreeLeafAdapter leafAdapter = new TreeLeafAdapter(leafs, leafData,
				inflater, this);
		TreeLeafItemClickListener leafItemClickListener = new TreeLeafItemClickListener(
				leafAdapter, ChooseAdressActivity.this,Index,source);
		treeview.setAdapter(leafAdapter);
		treeview.setOnItemClickListener(leafItemClickListener);
	}
	/**
	 * 查询数据
	 */
	public void queryDate() {
		leafs = new ArrayList<LeafDao>();
		leafData = new ArrayList<LeafDao>();

		getAdress sql = new getAdress();
		SQLiteDatabase database;
		database = sql.openDatabase(this);

		StringBuffer text = new StringBuffer();
		Cursor cursor = database.rawQuery("select * from T_Province", null);
		while (cursor.moveToNext()) {
			int i = 0;
			String name = cursor.getString(cursor
					.getColumnIndexOrThrow("ProName"));
			LeafDao e1 = new LeafDao(new Leaf(name, 4), LeafDao.TOP_LEVEL, i,
					LeafDao.NO_PARENT, true, false);
			leafs.add(e1);
			i = i + 1;
		}
		database.close();
	}


    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.back:
            Intent intent = new Intent(ChooseAdressActivity.this,
                    PersonalInformationActivity.class);
            startActivity(intent);
            break;
        default:
            break;
    }
    }
}
